#include "PhoneCard.h"
#include <map>

class PhoneBook {
public:
	PhoneBook& CardAdd(string,PhoneCard);    //add a person to the phonebook
	PhoneBook& CardDel(string);              //del a person from the phonebook  
	PhoneCard CardSearch(string);            //search a person in the phonebook
	void ShowBook();                            
private:
	map<string,PhoneCard> phonebook;         //define a map to store name and its information!
};

PhoneBook& PhoneBook::CardAdd(string add_name,PhoneCard phone_card) {
	phonebook.insert(make_pair(add_name,phone_card));
	return *this;
}

PhoneBook& PhoneBook::CardDel(string del_name) {
	if(phonebook.erase(del_name)) {
		cout << "OK:" << del_name << " removed!\n";
		return *this;
	} else {
		cout << "Sorry:" << del_name << " not found!\n";
		return *this;
	}
}

PhoneCard PhoneBook::CardSearch(string search_name) {
	map<string,PhoneCard>::iterator it=phonebook.find(search_name);
	if(it!=phonebook.end()) {
		cout << "OK:" << search_name << " is in the phone book!\n";
		return it->second;
	} else {
		cout << "Sorry:" << search_name << " is not in the phone book!\n";
		PhoneCard empty;                                  
		return empty;                           //Is it right to return such an empty type in this situation,Is there a better way?
	}

}

void PhoneBook::ShowBook() {
	for(map<string,PhoneCard>::iterator it=phonebook.begin();
			it!=phonebook.end();++it) {
		cout << it->first << ":\n";
		(it->second).ShowCard();
		cout << "\n\n";
	}
}
